﻿using Engine;
using Game;
using GameEntitySystem;
using TemplatesDatabase;



public class 生物行为属性 : ComponentBehavior, IUpdateable
{
    public SubsystemModelsRenderer subsystemModelsRenderer;
    private ComponentHealth m_componentHealth;
    public ComponentCreature m_componentCreature;
    public ComponentChaseBehavior m_componenttChaseBehavior;
    public SubsystemTerrain m_subsystemTerrain;
    private StateMachine stateMachine = new StateMachine();
    public SubsystemTime m_subsystemTime;
    public SubsystemProjectiles m_subsystemProjectiles;
    public Game.Random m_random = new Game.Random();
    public double m_nextUpdateTime;
    public double m_ChargeTime;
    public float m_distance;
    public ulong m_seed;
    public SubsystemAudio m_subsystemAudio;
    public SubsystemSky m_subsystemSky;
    public SubsystemParticles m_subsystemParticles;
    public Vector3 oldBoxSize;


    public UpdateOrder UpdateOrder => UpdateOrder.Default;

    public override float ImportanceLevel
    {
        get
        {
            return 0;
        }
    }

    public StateMachine StateMachine { get => stateMachine; set => stateMachine = value; }

    public int Int()
    {
        m_seed = ((m_seed * 25214903917L + 11) & 0xFFFFFFFFFFFF);
        return (int)(m_seed >> 17);
    }

    public int UniformInt(int min, int max)
    {
        return (int)(min + Int() * (long)(max - min + 1) / 2147483648L);
    }

    public float UniformFloat(float min, float max)
    {
        float num = Int() / 2.147484E+09f;
        return min + num * (max - min);
    }

    public override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap)
    {
        base.Load(valuesDictionary, idToEntityMap);
        m_componentCreature = Entity.FindComponent<ComponentCreature>(true);
        m_componenttChaseBehavior = Entity.FindComponent<ComponentChaseBehavior>(true);
        m_subsystemTerrain = Project.FindSubsystem<SubsystemTerrain>(true);
        m_subsystemTime = Project.FindSubsystem<SubsystemTime>(true);
        m_subsystemProjectiles = Project.FindSubsystem<SubsystemProjectiles>(true);
        m_componentHealth = Entity.FindComponent<ComponentHealth>(true);
        m_subsystemSky = Project.FindSubsystem<SubsystemSky>(throwOnError: true);
        m_subsystemParticles = Project.FindSubsystem<SubsystemParticles>(throwOnError: true);
        oldBoxSize = m_componentCreature.ComponentBody.BoxSize;
    }

    public void Update(float dt)
    {


        if (m_componentHealth.AttackResilience >= 6000 && m_componentHealth.AttackResilience < 7000 && m_componentHealth.Health >= 1)
        {
            m_distance = 10;
            if (m_componenttChaseBehavior.Target != null && m_subsystemTime.PeriodicGameTimeEvent(5.0, 0.0))
            {
                m_componentCreature.Project.FindSubsystem<SubsystemSky>().MakeLightningStrike(m_componenttChaseBehavior.Target.ComponentBody.Position + m_random.Vector3(0.5f));
            }
            StateMachine.Update();
            m_ChargeTime = UniformFloat(5.5f, 6.5f);
            if (m_distance < 10) m_ChargeTime *= 0.7;
            m_nextUpdateTime = m_subsystemTime.GameTime + m_ChargeTime;
        }

        if (m_componentHealth.AttackResilience >= 3000 && m_subsystemTime.GameTime >= m_nextUpdateTime && m_componentHealth.Health >= 1)
        {
            m_distance = 10;
            if (m_componentHealth.Health > 0f && m_subsystemTime.PeriodicGameTimeEvent(1.0, 0.0))
            {
                m_distance = 10;
                if (m_componenttChaseBehavior.Target != null && m_subsystemTime.PeriodicGameTimeEvent(1.0, 0.0))
                {
                    Vector3 position = m_componentCreature.ComponentCreatureModel.EyePosition + m_componentCreature.ComponentBody.Matrix.Right * 0.3f - m_componentCreature.ComponentBody.Matrix.Up * 0.2f + m_componentCreature.ComponentBody.Matrix.Forward * 0.2f;
                    Vector3 target_direction = m_componenttChaseBehavior.Target.ComponentBody.Position - position;
                    m_distance = target_direction.Length();
                    Vector3 direction = Vector3.Normalize(target_direction + m_random.Vector3((m_distance < 10) ? 0.4f : 1f));
                    int value = Terrain.MakeBlockValue(529);//ArrowType:0、1、2、3、4、8
                    float vx = MathUtils.Lerp(0f, 40f, MathUtils.Pow((float)m_ChargeTime / 2f, 0.5f));
                    m_subsystemProjectiles.FireProjectile(value, position, direction * vx + new Vector3(0, 8 * m_distance / vx, 0), Vector3.Zero, m_componentCreature);
                    Projectile projectile = m_subsystemProjectiles.FireProjectile(value, position, direction * vx + new Vector3(0, 8 * m_distance / vx, 0), Vector3.Zero, m_componentCreature);

                    m_subsystemProjectiles.AddTrail(projectile, Vector3.Zero, new SmokeTrailParticleSystem(20, 1f, float.MaxValue, new Color(255f, 255f, 0f)));
                    AudioManager.PlaySound("Audio/sj", 1f, 0f, 0f);
                }
                m_ChargeTime = UniformFloat(0.5f, 1.5f);
                if (m_distance < 10) m_ChargeTime *= 0.7;
                m_nextUpdateTime = m_subsystemTime.GameTime + m_ChargeTime;
                StateMachine.Update();
            }
        }


        if (m_componentHealth.AttackResilience >= 4000 && m_subsystemTime.GameTime >= m_nextUpdateTime && m_componenttChaseBehavior.Target != null)
        {
            Vector3 target_direction = m_componenttChaseBehavior.Target.ComponentBody.Position - m_componentCreature.ComponentBody.Position;
            m_distance = target_direction.Length();
            if (m_subsystemTime.PeriodicGameTimeEvent(1.0, 0.0) && m_distance > 3)
            {
                m_componentCreature.Project.FindSubsystem<SubsystemSky>().MakeLightningStrike(m_componenttChaseBehavior.Target.ComponentBody.Position + m_random.Vector3(0.5f));
            }
        }

        if (m_componentHealth.AttackResilience >= 5000 && m_subsystemTime.GameTime >= m_nextUpdateTime && m_componenttChaseBehavior.Target != null)
        {
            if (m_subsystemTime.PeriodicGameTimeEvent(5.0, 0.0))
            {
                m_componentCreature.ComponentHealth.Health += 0.05f;
            }
        }

        if (m_componentHealth.AttackResilience >= 6000 && m_subsystemTime.GameTime >= m_nextUpdateTime && m_componenttChaseBehavior.Target != null)
        {
            if (m_subsystemTime.PeriodicGameTimeEvent(5.0, 0.0))
            {
                m_componentCreature.ComponentHealth.Health += 0.1f;
            }
        }

        if (m_componentHealth.AttackResilience >= 7000 && m_subsystemTime.GameTime >= m_nextUpdateTime && m_componenttChaseBehavior.Target != null)
        {
            if (m_componentCreature.ComponentHealth.Health < 0.1f)
            {
                if (m_subsystemTime.PeriodicGameTimeEvent(15.0, 0.0))
                {
                    m_componentCreature.ComponentHealth.Health += 0.5f;
                }
            }
        }


        ComponentPlayer componentPlayer = m_componenttChaseBehavior.Target.Entity.FindComponent<ComponentPlayer>();
        float YZ = m_componentCreature.ComponentBody.Position.Y - m_componenttChaseBehavior.Target.ComponentBody.Position.Y;
        NK9 hL = componentPlayer.Entity.FindComponent<NK9>();
        if (componentPlayer != null)
        {
            hL.Y = m_componentCreature.ComponentHealth.Health;

        }
        if (m_componentCreature.ComponentHealth.AttackResilience > 500)
        {
            if (m_componenttChaseBehavior.Target != null && YZ < 10)
            {
                float x = m_componentCreature.ComponentBody.StanceBoxSize.Y - 0.05f;
                m_subsystemParticles.AddParticleSystem(new PlayerParticle1(m_subsystemTerrain, m_componentCreature.ComponentBody.Position, x));
                m_componentCreature.ComponentLocomotion.FlySpeed = 6f;
               // m_componentCreature.ComponentHealth.FallResilienceFactor = 1000f;
               // m_componentCreature.ComponentHealth.FireResilienceFactor = 1000f;
                if (m_componentCreature.ComponentHealth.Health < 0.2f)
                {
                    m_componentCreature.ComponentLocomotion.FlySpeed = 0.5f;
                }
            }
        }

        Vector3 Position2 = m_componentCreature.ComponentBody.Position;
        if (m_componentHealth.Health <= 0f)
        {
            if (m_subsystemTime.PeriodicGameTimeEvent(80.0, 0.0))
            {
                Project.RemoveEntity(m_componentCreature.Entity, true);
            }

            if (m_componentHealth.AttackResilience < 200 && m_subsystemTime.PeriodicGameTimeEvent(5.0, 0.0))
            {
                Project.FindSubsystem<SubsystemPickables>(throwOnError: true).AddPickable(570, 1, Position2, default, default);
                Project.FindSubsystem<SubsystemPickables>(throwOnError: true).AddPickable(586, 1, Position2, default, default);
            }
            if (m_componentHealth.AttackResilience >= 200 && m_componentHealth.AttackResilience < 1000 && m_subsystemTime.PeriodicGameTimeEvent(5.0, 0.0))
            {
                Project.FindSubsystem<SubsystemPickables>(throwOnError: true).AddPickable(571, 1, Position2, default, default);
            }
            if (m_componentHealth.AttackResilience >= 1000 && m_componentHealth.AttackResilience < 2000 && m_subsystemTime.PeriodicGameTimeEvent(5.0, 0.0))
            {
                Project.FindSubsystem<SubsystemPickables>(throwOnError: true).AddPickable(572, 1, Position2, default, default);
            }
            if (m_componentHealth.AttackResilience >= 2000 && m_componentHealth.AttackResilience < 3000 && m_subsystemTime.PeriodicGameTimeEvent(5.0, 0.0))
            {
                Project.FindSubsystem<SubsystemPickables>(throwOnError: true).AddPickable(573, 1, Position2, default, default);
            }
            if (m_componentHealth.AttackResilience >= 3000 && m_componentHealth.AttackResilience < 4000 && m_subsystemTime.PeriodicGameTimeEvent(5.0, 0.0))
            {
                Project.FindSubsystem<SubsystemPickables>(throwOnError: true).AddPickable(574, 1, Position2, default, default);
            }
            if (m_componentHealth.AttackResilience >= 4000 && m_componentHealth.AttackResilience < 5000 && m_subsystemTime.PeriodicGameTimeEvent(5.0, 0.0))
            {
                Project.FindSubsystem<SubsystemPickables>(throwOnError: true).AddPickable(575, 1, Position2, default, default);
            }
            if (m_componentHealth.AttackResilience >= 5000 && m_componentHealth.AttackResilience < 6000 && m_subsystemTime.PeriodicGameTimeEvent(5.0, 0.0))
            {
                Project.FindSubsystem<SubsystemPickables>(throwOnError: true).AddPickable(575, 1, Position2, default, default);
            }
            if (m_componentHealth.AttackResilience >= 6000 && m_componentHealth.AttackResilience < 10000 && m_subsystemTime.PeriodicGameTimeEvent(5.0, 0.0))
            {
                Project.FindSubsystem<SubsystemPickables>(throwOnError: true).AddPickable(576, 1, Position2, default, default);
            }
            if (m_componentHealth.AttackResilience >= 6000 && componentPlayer != null)
            {
                Project.FindSubsystem<SubsystemPickables>(throwOnError: true).AddPickable(576, 1, Position2, default, default);
            }
        }
    }
    public 生物行为属性() : base() { }
}